package com.hzy.seataaccount.account.service.impl;

import com.hzy.account.entities.Account;
import com.hzy.seataaccount.account.dao.AccountMapper;
import com.hzy.seataaccount.account.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 这里填写描述
 * 填写修改说明
 *
 * @author hzy
 * @version Revision:v1.0
 * @since Date:2022-03-15 16:56
 */
@Slf4j
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Account decrease(String userId, BigDecimal money) {
        log.info("----------> 扣减账户开始, userId:{}, money:{}", userId, money);
        if (money.compareTo(BigDecimal.ZERO) < 0){
            throw new RuntimeException("扣款失败, 金额小于 0");
        }
        Account account = accountMapper.selectByUserId(userId);
        account.setMoney(account.getMoney().subtract(money));
        int result = accountMapper.updateByPrimaryKey(account);
        log.info("----------> 扣减账户结束:{}, {}", result, account);
        return account;
    }
}
